package com.eam.controller;

import com.eam.service.EquipmentService;
import com.mes.entity.User;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("equipment")
public class EquipmentController {
	@Autowired
	private EquipmentService equipmentService;
	
	//加载保养计划编码
	@ResponseBody
	@RequestMapping(value="initProcessInfo",method=RequestMethod.POST)
	public Message initProcessInfo(HttpServletRequest request){
		Message msg = new Message();
		
		String orgId = (String)request.getSession().getAttribute("orgId");    //获取当前登录人是几厂的
		
		if(orgId == null ){
			msg.setData("Plans",equipmentService.getEquipmentPlanMaintainList());
		}else{
		
			msg.setData("Plans",equipmentService.getEquipmentPlanMaintainLists(orgId));
		}
		
		
		return msg;
	}
	
	//加载维修计划编码
	@ResponseBody
	@RequestMapping(value="getEquipmentPlanRepairList",method=RequestMethod.POST)
	public Message getEquipmentPlanRepairList(HttpServletRequest request){
		Message msg = new Message();
		
		String orgId = (String)request.getSession().getAttribute("orgId");    //获取当前登录人是几厂的
		
		
		if(orgId == null ){
			msg.setData("Repairs",equipmentService.getEquipmentPlanRepairList());
		}else{
			
			msg.setData("Repairs",equipmentService.getEquipmentPlanRepairLists(orgId));
		}
		
		return msg;
	}
	
	//根据计划编码加载保养设备编码
	@ResponseBody
	@RequestMapping(value="loadCollectionByProcessId",method=RequestMethod.POST)
	public Message loadCollectionByProcessId(HttpServletRequest request){
		String code = request.getParameter("CODE");
		Message msg = new Message();		
		msg.setData("failices",equipmentService.getEquipmentList(code));
		return msg;
	}
	
	
	//根据计划编码加载维修设备编码以及维修设备信息和维修项目
	@ResponseBody
	@RequestMapping(value="loadCollectionCheck",method=RequestMethod.POST)
	public Message loadCollectionCheck(HttpServletRequest request){
		String code = request.getParameter("CODE");
		Message msg = new Message();		
		msg.setData("Maintenance",equipmentService.getmaintenanceList(code));
		return msg;
	}
	
	
	//根据计划编码和设备编码加载设备信息和保养项目
	@ResponseBody
	@RequestMapping(value="EquipmentInformation",method=RequestMethod.POST)
	public Message EquipmentInformation(HttpServletRequest request){
		String code = request.getParameter("CODE");
		String plancode = request.getParameter("PLANCODE");
		Message msg = new Message();		
		msg.setData("information",equipmentService.getinformationList(plancode,code));
		return msg;
	}
	
	
	//保存设备保养信息
	@ResponseBody
	@RequestMapping(value="EquipmentPreservation",method=RequestMethod.POST)
	public Message EquipmentPreservation(HttpServletRequest request){
		Message msg = new Message();
		
		try {
			
		String emesmaintainid = null;
		String emesmaintainplanid = null;
		String facilitiesid =null;
		String code = request.getParameter("CODE");				//设备编码
		String plancode = request.getParameter("PLANCODE");		//维修保养计划编码
		String values = request.getParameter("CHESTR");			//维修保养项目
		String Notes =  request.getParameter("Remarks");	   //保养维修项目备注说明的值
		String Equipmenttype = request.getParameter("Equipmenttype"); //维修保养类型区分
		
		String [] idss = values.split(",");
		String [] Remarks = Notes.split(",");
		
		int numbers = idss.length;
	
		User user = UserUtil.getLoginUser(request);
		
		String adorgId = user.getOrgId();         //登录人的ad_org_id
		String adclientId = user.getClientId();   //登录人的ad_client_id
		String userIdtime = user.getId();			//登录人的ad_user_id		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间

		List<Map<String, Object>> ListUser = equipmentService.getEquipmentputCreateby(userIdtime);

		String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy

		String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy


		for(int j =0 ; j<numbers ;  j++ ){
			String name =idss[j];
			List<Map<String, Object>> detailsids ;

				detailsids = equipmentService.getEquipmentputs(name,code,plancode); //查出项目的ID


			String RemarksNames = "";
			String id = detailsids.get(0).get("SBID").toString();
			String planid = detailsids.get(0).get("PLANID").toString();


			if(Remarks.length == 0){

			}else{
				for(int o = 0 ; o<Remarks.length ; o++){
					if(o == j){
						RemarksNames = Remarks[o];
					}
				}

			}
			equipmentService.getEquipmentputUpdate(id);
			equipmentService.SaveEquipmentPlanDetails(id,RemarksNames);
			equipmentService.SaveEquipmentUserDetails(id, userIdtime);

			List<Map<String, Object>> Allresults =  equipmentService.getAllEquipemt(id);

			facilitiesid = Allresults.get(0).get("FACILITIESID").toString();

			emesmaintainplanid = Allresults.get(0).get("EmesMAINTAINPLANID").toString();

			String Types = "MAINTAIN";
			String status = "FINISHED";

			if(Equipmenttype.equals("maintain")){
			 emesmaintainid = Allresults.get(0).get("EmesMAINTAINID").toString();
			 equipmentService.InsertEquipmentputInserts(time,facilitiesid,emesmaintainplanid,userIdtime,emesmaintainid,Types,status,RemarksNames,adorgId,adclientId,createBy,updatedBy);
			 equipmentService.getEquipmentputList(id,planid);
		}
		}

		} catch (Exception e) {
			msg.setData("results","false");
			e.printStackTrace();
		}
		return msg;
	}


	//保存设备维修信息
		@ResponseBody
		@RequestMapping(value="saveEquipmentRepairation",method=RequestMethod.POST)
		public Message saveEquipmentRepairation(HttpServletRequest request){
			Message msg = new Message();

			try {

				String code = request.getParameter("CODE1");				//设备编码
				String plancode = request.getParameter("PLANCODE1");		//维修计划编码
				String Equipmenttype = request.getParameter("Equipmenttype"); //维修保养类型区分
				String gztypes = request.getParameter("gztypes1"); //故障原因
				String method = request.getParameter("method1"); //处理方法
				String Measures = request.getParameter("Measures1"); //改善措施
				String orgname = request.getParameter("orgname1"); //责任单位
				String Remarks = request.getParameter("Remarks1"); //备注
				//String proposal = request.getParameter("proposal1"); //维修建议
				String bxuserids = request.getParameter("bxuserids"); //报修人的AD_USER_ID
				String repairName = request.getParameter("repairName");//报修人
				String repairtime = request.getParameter("repairtime");//维修时间
				double repairtimeVal = 0d;
				if(StringUtils.isNotEmpty(repairtime)){
					repairtimeVal = Double.parseDouble(repairtime);
				}

				User user = UserUtil.getLoginUser(request);

				String adorgId = user.getOrgId();         //登录人的ad_org_id
				String adclientId = user.getClientId();   //登录人的ad_client_id
				String userIdtime = user.getId();			//登录人的ad_user_id
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间
				
				List<Map<String, Object>> ListUser = equipmentService.getEquipmentputCreateby(userIdtime);
				
				String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy
				
				String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy
				
				
				List<Map<String, Object>> item = equipmentService.getEquipmentputRepoir(code,plancode); //查询维修设备层的ID
				
				String id = item.get(0).get("ITEMID").toString();   //item层ID
				
				String facilitiesid = item.get(0).get("SBID").toString(); //维修设备ID
				
				String planId = item.get(0).get("PLANID").toString(); //维修计划ID
				
				String sbname = item.get(0).get("SBNAME").toString(); //维修设备名称
				
				String Type = "REPAIRMENT";
				
				String status = "FINISHED";
				String OK = "OK";
				equipmentService.SaveEquipmentUserItem(id,gztypes,method,Measures,orgname,Remarks,OK,userIdtime,repairtimeVal);
				equipmentService.SaveEquipmentPlan(planId,OK,time);
				
				equipmentService.SavaEquipmentputInsert(time,facilitiesid,planId,userIdtime,Type,status,Remarks,adorgId,adclientId,createBy,updatedBy);
				//equipmentService.getEquipmentputLists(id);
			/*	String messageText = "报修人："+repairName+" 维修编码："+plancode+"\r\n"
									 +"设备编码名称："+code+"——"+sbname+"\r\n"
									 +"故障原因："+gztypes+"\r\n"
									 +"处理方法："+method+"\r\n"
						 			 +"改善措施："+Measures+"\r\n"
									 +"备注："+Remarks;
				SendMessageUtil.sendMessage("SBWX", "设备维修", messageText,adorgId,"");*/
			
			} catch (Exception e) {
				msg.setData("results","false");
				e.printStackTrace();
			}
			return msg;
		}
	
}
